package it.mp.multitask.bean;

import it.mp.multitask.R;
import android.app.Activity;
import android.app.ActivityManager.RecentTaskInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class App {
	private String packageName;
	private String className;

	public App(RecentTaskInfo reti) {
		packageName = reti.baseIntent.getComponent().getPackageName();
		className = reti.baseIntent.getComponent().getClassName();
	}
	public App(RunningTaskInfo rti) {
		packageName = rti.baseActivity.getPackageName();
		className = rti.baseActivity.getClassName();
	}
	public App(String packageName, String className) {
		this.packageName = packageName;
		this.className = className;
	}
	public String getPackageName() {
		return packageName;
	}
	public String getClassName() {
		return className;
	}

	public View getView(Activity parent, OnClickListener oclStartActivity, OnLongClickListener oclKillActivity) throws NameNotFoundException {
		TextView tv = null;
		ImageView iv = null;
		Drawable img = null;
		View vApp = null;
		img = parent.getPackageManager().getApplicationIcon(packageName);
		if (oclKillActivity==null){
			vApp = parent.getLayoutInflater().inflate(R.layout.app_recently, null);
		}else{
			vApp = parent.getLayoutInflater().inflate(R.layout.app_running, null);
		}
		iv = (ImageView) vApp.findViewById(R.id.IV_icon);
		iv.setOnClickListener(oclStartActivity);
		iv.setOnLongClickListener(oclKillActivity);
		iv.setTag(this);
		iv.setOnClickListener(oclStartActivity);
		tv = (TextView) vApp.findViewById(R.id.TV_label);
		iv.setImageDrawable(img);
		tv.setText(parent.getPackageManager().getApplicationLabel(parent.getPackageManager().getApplicationInfo(packageName, 0)));
		tv.setVisibility(View.VISIBLE);
		vApp.setMinimumWidth(60);
		return vApp;
	}
	public View getView(Activity parent, OnClickListener oclStartActivity) throws NameNotFoundException {
		return getView(parent, oclStartActivity, null);
	}

}
